import { observable, action,toJS } from 'mobx';
import { Browser, RouteLayout, Upload,Reply } from 'weaver-mobile-page';
import { Toast,Bubble,Tools } from 'weaver-mobile';
import * as Api from '../../api/plan/planViewApi';
import * as util from '../../util';
const getLabel = Tools.getLabel
export default class PlanDetailStore {
    detailPath = '/planHome/planView/index/detail'
    //任务 文档 流程 客户 项目 
    @observable taskBrowser = new Browser.Store({ route: this.detailPath, id: 'taskBrowser' });
    @observable taskBrowserValue = [];

    @observable docBrowser = new Browser.Store({ route: this.detailPath, id: 'docBrowser' });
    @observable docBrowserValue = [];

    @observable wfBrowser = new Browser.Store({ route: this.detailPath, id: 'wfBrowser' });
    @observable wfBrowserValue = [];

    @observable customerBrowser = new Browser.Store({ route: this.detailPath, id: 'customerBrowser' });
    @observable customerBrowserValue = [];

    @observable projectBrowser = new Browser.Store({ route: this.detailPath, id: 'projectBrowser' });
    @observable projectBrowserValue = [];

    //detail 
    @observable detailLayoutStore = new RouteLayout.Store({ route: '/planHome/planView/index', id: 'detail' });
    @observable detailFileStore = new Upload.Store({ route: '/planHome/planView/index/detail' });
    @observable detailMapData = {}
    @observable detailMapList = []
    @observable detailId = ''
    @observable detailFileIds = ''
    @observable detailFiles = []
    @observable maxsize = ''
    @observable category=''
    @observable detailFbList = []
    @observable detailReplyStore = new Reply.Store();
    @observable isSaving = false;
    @observable isLoading =false
    //获取报告详情
    @action getDetailView = (params) => {
        Toast.loading('loading', 0)
        this.isLoading = true
        this.detailMapData={}
        return Api.getDetailView(params).then(res => {
            Toast.hide()
            if (res.api_status == '1') {
                this.detailMapData = res.mapdata
                this.detailMapList = res.customList
                this.detailId = res.plandetailid
                this.canedit=res.canedit
                this.detailFbList = res.fblist
                if (res.mapdata && res.mapdata.filearray) {
                    this.detailFiles = util.formatFiles(res.mapdata.filearray,res.canedit)
                }else{
                    this.detailFiles = []
                }
                this.detailFileIds = res.mapdata.fileids?res.mapdata.fileids:''
                this.maxsize = res.maxsize
                this.category=res.category
            }else{
                Bubble.error(getLabel('508818', '接口调用失败！'), 3,null,null,null,false);
            }
            this.isLoading = false
            return res
        }).catch(res=>{this.isLoading=false});
    }
    //保存报告详情
    @action saveDetailView = (params,showToast) => {
        Toast.loading('loading', 0)
        this.isSaving = true
        return Api.saveDetailView(params).then(res => {
            this.isSaving=false
            if (res.api_status == '1') {
                showToast && Bubble.success(getLabel('30700','操作成功') , 3,null,null,null,false);
                if(params.operation=='add_feedback'){
                    let list  = toJS(this.detailFbList)
                    if(!list || list.length<1){
                        list = [res.datamap]
                    }else{
                        list.unshift(res.datamap)
                    }
                    this.setState({detailFbList:list})
                    this.detailReplyStore.doClear();
                }
                
            }else{
                Bubble.error(getLabel('508818', '接口调用失败！'), 3,null,null,null,false);
            }
            Toast.hide()
            return res
        }).catch(res=> {
            this.isSaving=false
            Bubble.error(getLabel('508818', '接口调用失败！'), 3,null,null,null,false);
            Toast.hide()
        });
    }
    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }
    @action
    reset = () => {
        this.taskBrowserValue = []
        this.docBrowserValue = [];
        this.wfBrowserValue = [];
        this.customerBrowserValue = [];
        this.projectBrowserValue = [];
        this.fileBrowserValue = [];

        this.taskBrowser = new Browser.Store({ route: this.detailPath, id: 'taskBrowser' });
        this.docBrowser = new Browser.Store({ route: this.detailPath, id: 'docBrowser' });
        this.wfBrowser = new Browser.Store({ route: this.detailPath, id: 'wfBrowser' });
        this.customerBrowser = new Browser.Store({ route: this.detailPath, id: 'customerBrowser' });
        this.projectBrowser = new Browser.Store({ route: this.detailPath, id: 'projectBrowser' });
    }
     //去掉无用的属性
  removeFieldsNoInColumns(v) {

    delete v['fbdate'];
    delete v['taskarray']
    delete v['filearray']
    delete v['docarray']
    delete v['wfarray']
    delete v['crmarray']
    delete v['projectarray']
    delete v['viewdate']
    if (v.id && (v.id.startsWith('temp') || v.id.indexOf("_") > -1)) {
        delete v['id'];
    }
    return v;
}
   

}